#ifndef XYLIB_MAP_H_20181002
#define XYLIB_MAP_H_20181002

#include <stdint.h>

#ifdef WIN32
#include <windows.h>
#endif

#define MAP_BLOCK_WIDTH		320
#define MAP_BLOCK_HEIGHT	240

class XY_Map {
public:
	struct MapInfo {
		uint32_t		width;
		uint32_t		height;
		uint32_t		blockWidth;
		uint32_t		blockHeight;
		uint32_t		blockOfRow;
		uint32_t		blockOfCol;
		uint32_t		blockCount;
		uint32_t		maskCount;
	};

	XY_Map();
	~XY_Map();
	XY_Map(XY_Map&) = delete;
	XY_Map& operator=(XY_Map&) = delete;

	bool		LoadMap(const char* fileName);
	void		FreeMap(void);
	bool		IsLoad(void) const;

	const MapInfo*	GetInfo(void) const;
	const void*		GetImage(uint32_t index, uint32_t *dataSize = nullptr);

	struct MapMaskInfo {
		uint32_t		x;
		uint32_t		y;
		uint32_t		width;
		uint32_t		height;
		uint32_t		blockIndex;
		uint8_t*		alpha;
		MapMaskInfo() {
			alpha = nullptr;
		}
		~MapMaskInfo() {
			if (alpha) {
				delete[] alpha;
				alpha = nullptr;
			}
		}
	};

	const MapMaskInfo* GetMaskInfo(void) const { return _pMaskInfo; }

	bool			IsObstacle(uint32_t x, uint32_t y) const;
	bool			IsEntry(uint32_t x, uint32_t y) const;
private:
	void			DecompressMask(void* in, void* out);
private:
	bool			_isLoad;
	void*			_ptrData;
	MapInfo			_info;
	uint32_t*		_pImage;
	MapMaskInfo*	_pMaskInfo;
	uint8_t*		_pCell;
	struct Image_Info 
	{
		unsigned char*	ptr;
		uint32_t		size;
		bool			isNew;
		Image_Info() {
			isNew = false;
			ptr = nullptr;
			size = 0;
		}
		~Image_Info();
	}*				_images;
#ifdef WIN32
	HANDLE			_hFile;
	HANDLE			_hFileMap;
#endif
};
#endif